//JQuery extensions for ajax functions
$.extend({
	$ajaxUri: null,
	$fbCode: null,
	initAjaxUri: function(type,uri) { //initializes ajax uri from property file
		if (type == "service") $ajaxServiceUri = uri;
		else if (type == "auth") $ajaxAuthUri = uri;
	},
	ajaxService: function(url,param,callback) { //makes a server side ajax call
		var ajaxUrl = $ajaxServiceUri + url;
		$.ajax({
			url: ajaxUrl,
			type: 'POST',
			data: param.jsonString(),
			success: function(data,status,jqXHR) {
				(data.error != null) ? (function() {
					$.showError(data.error);
				}).call() : callback(data,status,jqXHR);
			},
			dataType: 'json',
			contentType: 'application/json'
		});
	},
	ajaxLogin: function(username,password) {
		var ajaxUrl = $ajaxAuthUri + "j_security_check?";
		ajaxUrl+= "j_username="+username;
		ajaxUrl+= "&j_password="+password;

		$.ajax({
			url: ajaxUrl,
			type: 'POST',
			success: function(data,status,jqXHR) {
				if(data == "error") $.showError("Invalid username or password");
				else window.location.href = "/";
			}
		});
	},
	ajaxLogout: function() {
		var ajaxUrl = $ajaxAuthUri + "logout";
		$.ajax({
			url: ajaxUrl,
			type: 'POST',
			success: function(data,status,jqXHR) {
				if(data == "logoutsuccess") window.location.href = "/";
				else $.showError("failed to logout");
			}
		});
	}
});